﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StockShark.Models;

namespace StockShark.ViewModels
{
    public class BAChartsData : ChartData
    {
        public List<AccountValue> Values { get; set; }
        public BrokerageAccount BrokerageAccount { get; set; }
        public decimal SharesValue { get; set; }
        public decimal CurrenciesValue { get; set; }
        public decimal MaterialsValue { get; set; }

        public void CalculateValues()
        {
            SharesValue = CurrenciesValue = MaterialsValue = 0;
            foreach (var oi in BrokerageAccount.OwnedInstruments)
            {
                if (oi.Paper.GetType().Name.Equals("Share"))
                    SharesValue += oi.TotalValue;
                else if (oi.Paper.GetType().Name.Equals("Currency"))
                    CurrenciesValue += oi.TotalValue;
                else if (oi.Paper.GetType().Name.Equals("Material"))
                    MaterialsValue += oi.TotalValue;
            }
        }

        public string GetFlotData()
        {
            string data = "[[";
            int step = (DateTime.Today - StartDate).Days / 200 + 1;
            foreach (var v in Values)
            {
                if (v.Timestamp.DayOfYear % step == 0)
                    data += "[" + (v.Timestamp - StartDate).Days + ", " + v.Value.ToString().Replace(',', '.') + "],";
            }
            data += "[" + (DateTime.Today - StartDate).Days + ", " + BrokerageAccount.TotalValue.ToString().Replace(',', '.') + "]";
            data += "]]";
            return data;
        }


        public string GetPieData()
        {
            string data = "[";
            data += "{ label: 'Wolne środki', data: " + BrokerageAccount.Balance.ToString().Replace(',', '.') + "}, ";
            data += "{ label: 'Akcje', data: " + SharesValue.ToString().Replace(',', '.') + "}, ";
            data += "{ label: 'Waluty', data: " + CurrenciesValue.ToString().Replace(',', '.') + "}, ";
            data += "{ label: 'Surowce', data: " + MaterialsValue.ToString().Replace(',', '.') + "}, ";
            data += "]";
            return data;
        }
    }
}